﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FileWatcher
{
    public class FileWatcher
    {
        FileSystemWatcher _watch = new FileSystemWatcher();
        public delegate void FileCreatedHandler(string FilePath);
        public event FileCreatedHandler FileCreated;

        public delegate void FileDeletedHandler(string FilePath);
        public event FileDeletedHandler FileDeleted;
        public FileWatcher(string path)
        {
            _watch.Path = path;
            _watch.Created += new FileSystemEventHandler(_watch_Created);
            _watch.Deleted += new FileSystemEventHandler(_watch_Deleted);
            _watch.EnableRaisingEvents = true;
        }
        private void _watch_Created(object sender, FileSystemEventArgs e)
        {
            if (FileCreated != null)
            {
                FileCreated(e.FullPath);
            }
            //sb = new StringBuilder();

            //dirInfo = new DirectoryInfo(e.FullPath.ToString());

            //sb.AppendLine("新建檔案於：" + dirInfo.FullName.Replace(dirInfo.Name, ""));
            //sb.AppendLine("新建檔案名稱：" + dirInfo.Name);
            //sb.AppendLine("建立時間：" + dirInfo.CreationTime.ToString());
            //sb.AppendLine("目錄下共有：" + dirInfo.Parent.GetFiles().Count() + " 檔案");
            //sb.AppendLine("目錄下共有：" + dirInfo.Parent.GetDirectories().Count() + " 資料夾");

            //MessageBox.Show(sb.ToString());
        }
        private void _watch_Deleted(object sender, FileSystemEventArgs e)
        {
            if (FileDeleted != null)
            {
                FileDeleted(e.FullPath);
            }
            //sb = new StringBuilder();

            //sb.AppendLine("被刪除的檔名為：" + e.Name);
            //sb.AppendLine("檔案所在位址為：" + e.FullPath.Replace(e.Name, ""));
            //sb.AppendLine("刪除時間：" + DateTime.Now.ToString());

            //MessageBox.Show(sb.ToString());
        }
    }
}
